<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/backend' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑评论</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="输入内容查找相似的评论或用户"
            class="input-with-select"
            v-model="commentarys.query"
            clearable
            @change="getCommentaryList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getCommentaryList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="dialogFormVisible = true">添加留言</el-button>
          <el-dialog title="添加留言" :visible.sync="dialogFormVisible">
            <el-form :model="addCommentarys" :rules="rules" ref="addCommentarys" label-width="100px" class="addCommentarys-form">
              <el-form-item label="主题类型" prop="topic_type">
                <el-select v-model="addCommentarys.topic_type" placeholder="请选择主题类别" @change="getTypeId(addCommentarys.topic_type)">
                  <el-option v-for="(ztItem,ztIndex) in zhutiList" :key="ztIndex" :label="ztItem.topic_type" :value="ztItem.topic_type"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="类型详情" prop="topic_id">
                <el-select v-model="addCommentarys.topic_id" placeholder="请选择类别详情">
                  <el-option v-for="zDItem in zhutiDetail" :key="zDItem.data_id" :label="zDItem.title" :value="zDItem.data_id"></el-option>
                </el-select>
                <!-- <el-input v-model="addCommentarys.topic_id"></el-input> -->
              </el-form-item>
              <el-form-item label="留言内容" prop="content">
                <el-input v-model="addCommentarys.content"></el-input>
              </el-form-item>
              <el-form-item label="置顶" prop="zd_id">
                <el-switch v-model="addCommentarys.zd_id"></el-switch>
              </el-form-item>
              <!-- <el-form-item label="置顶" prop="zd_id">
                <el-input v-model="addCommentarys.zd_id"></el-input>
              </el-form-item> -->
              <el-form-item label="置顶时长" prop="duration">
                <!-- <el-input v-model="addCommentarys.zd_days"></el-input> -->
                <el-input-number v-model="addCommentarys.duration" controls-position="right" @change="handleChange" :min="0" :max="60"></el-input-number>
                <el-select v-model="value" clearable placeholder="请选择" style="width:90px">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submitForm('addCommentarys')">发布</el-button>
                <el-button @click="resetForm('addCommentarys')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-dialog>
          </el-col>
      </el-row>
      <el-table stripe border :data="commentaryList" height="300" >
        <el-table-column type="index" label="编号"></el-table-column>
        <el-table-column prop="topic_id" label="主题id"></el-table-column>
        <el-table-column prop="topic_type" label="主题类型"></el-table-column>
        <el-table-column prop="content" label="评论内容"></el-table-column>
        <el-table-column prop="zd_id" label="是否置顶"></el-table-column>
        <el-table-column prop="zd_duration" label="置顶时长"></el-table-column>
        <el-table-column prop="praiseCount" label="点赞数"></el-table-column>
        <el-table-column prop="time" label="发表时间"></el-table-column>
        <el-table-column prop="account" label="发表用户账号"></el-table-column>
        <el-table-column prop label="操作" width="180">
          <template slot-scope="scope">
            <div>
              <!-- {{scope.row}} -->
              <!--修改-->
              <el-button type="primary" icon="el-icon-edit" circle @click="editOpen(scope.row)"></el-button>
              <!-- 弹出框，修改-->
              <el-dialog title="修改留言" :visible.sync="deitFormVisible" :modal-append-to-body='false'>
                <el-form :model="editCommentarys" :rules="rules" ref="editCommentarys" label-width="100px" class="Posts-form">
                  <el-form-item label="主题类型" prop="topic_type">
                    <el-select v-model="editCommentarys.topic_type" placeholder="请选择主题类别" @change="getTypeId(editCommentarys.topic_type)">
                      <el-option v-for="(eztItem,eztIndex) in zhutiList" :key="'ezin'+eztIndex" :label="eztItem.topic_type" :value="eztItem.topic_type"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="类型详情" prop="topic_id">
                    <el-select v-model="editCommentarys.topic_id" placeholder="请选择类别详情">
                      <el-option v-for="(ezDItem,ezDIndex) in zhutiDetail" :key="'info-'+ezDIndex" :label="ezDItem.title" :value="ezDItem.data_id"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="留言内容" prop="content">
                    <el-input v-model="editCommentarys.content"></el-input>
                  </el-form-item>
                  <el-form-item label="置顶" prop="zd_id">
                    <el-switch v-model="editCommentarys.zd_id"></el-switch>
                  </el-form-item>
                  <el-form-item label="置顶时长" prop="duration">
                    <input type="number"  min="0" max="60" style="width:90px;height:30px;">
                    <!-- <el-input-number v-model="editCommentarys.duration" controls-position="right" @change="handleChange" :min="0" :max="60"></el-input-number> -->
                    <el-select v-model="value" clearable placeholder="请选择" style="width:90px">
                      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="edit(editCommentarys)">修改</el-button>
                    <!-- <el-button @click="resetForm('editCommentarys')">重置</el-button> -->
                  </el-form-item>
                </el-form>
              </el-dialog>
              <!--回复-->
              <el-button type="primary" icon="el-icon-chat-line-square" circle @click="reply()"></el-button>
              <!--删除-->
              <el-button type="danger" icon="el-icon-delete" circle slot="reference"  @click="dialogVisible = true"></el-button>
              <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
                :modal-append-to-body='false'>
                <span>确定删除？</span>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="del(scope.row.mes_id)">确 定</el-button>
                </span>
              </el-dialog>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="commentarys.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="commentarys.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      commentarys: {
        query: "",
        pagenum: 1, //当前页码
        pagesize: 3 //每页显示条数
      },
      addCommentarys: {
        mes_id:"",
        topic_id:"",
        topic_type:"",
        content:"",
        zd_id:0,
        duration:"",
        zd_duration:"",
        time:"",
        from_uid:"",
        to_uids:"",
      },
      editCommentarys:{
        mes_id:"",
        topic_id:"",
        topic_type:"",
        content:"",
        zd_id:"",
        duration:"",
        zd_duration:"",
        praiseType:"",
        praiseCount:"",
        time:"",
        from_uid:"",
        to_uids:"",
      },
      commentaryList: [],
      total: 0,
      zhutiList:[],
      zhutiDetail:[],
      options: [{
          value: 'years',
          label: '年'
        },{
          value: 'months',
          label: '月'
        },{
          value: 'days',
          label: '日'
        }, {
          value: 'hours',
          label: '时'
        }, {
          value: 'minutes',
          label: '分'
        }, {
          value: 'seconds',
          label: '秒'
        }],
      value: 'days',
      typeId:[],
      rules: {
        sortName: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        content:[
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur' }
        ],
        topic_type:[
          { required: true, message: '请选择主题类型', trigger: 'change' }
        ],
        topic_id:[
          { required: true, message: '请选择类型详情', trigger: 'change' }
        ],

      },
      dialogFormVisible: false,//添加标签
      deitFormVisible:false,//修改标签
      dialogVisible: false,//删除标签
      isLogin:"",
      user_id:"",
    };
  },
  created() {
    this.getCommentaryList();
    this.getTopicid();
    this.isLogin = this.$store.getters.getShow;
    if(this.isLogin){
      this.user_id = this.$store.getters.getUser.user_id;
      //console.log(this.user_id);
    }
  },
  methods: {
    // 获取指定数据
    async getCommentaryList() {
      let commentarys = this.commentarys;
      console.log(commentarys);
      const {data:res} = await this.$http.post("selCommentarys",{commentarys});
      console.log(res);
     if (res.status !== 200) {
        this.$message.error("获取用户列表失败");
      } else {
        this.commentaryList = res.message;
        if(commentarys.query == ""){
          const {data:totalRes} = await this.$http.get("getCommentarys");
          console.log(totalRes.message.length);
          if(totalRes.status != 200){
            this.$message.error("获取用户列表失败");
          }else{
            this.total = totalRes.message.length;
          }
        }else{
          this.total = res.message.length;
        }
      }
    },
    // 查询全部的主题类别数据
    async getTopicid(){
      const {data:res} = await this.$http.get("selMesTypeId");
      console.log(res);
      if(res.status != 200){
        this.$message.error("获取用户列表失败");
      }else{
        // const typeId = res.message;
        this.zhutiList = res.message;
      }
    },
    // 查询类别详情
    async getTypeId(topic_type){
      console.log(topic_type);
      this.addCommentarys.topic_id = ""
      if(topic_type=="详情"){
        // console.log("查询详情信息");
        const {data:res} = await this.$http.get("getDataDesc");
        console.log(res);
        if(res.status != 200){
          this.$message.error("获取用户列表失败");
        }else{
          this.zhutiDetail = res.message;
        }
      }else{
        let obj = {};
        obj.data_id = 0;
        obj.title = "默认"
        this.zhutiDetail.push(obj);
        this.addCommentarys.topic_id = 0
        console.log(this.zhutiDetail);
      }
    },

    // 添加留言按钮
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.Publish();
        } else {
          this.$message.error('error submit!!');
          return false;
        }
      });
    },
    // 添加留言
    async Publish(){
      this.dialogFormVisible = false;
      this.addCommentarys.time = this.fileType.changeDate(new Date());
      this.addCommentarys.from_uid = this.user_id;
      // console.log(this.addCommentarys.duration);
      // console.log(this.changeDuration(this.addCommentarys.duration));
      this.addCommentarys.zd_duration = this.changeDuration(this.addCommentarys.duration);
      let mes = JSON.stringify(this.addCommentarys);
      console.log(mes);
      const {data:res} = await this.$http.post("insertMessage",{mes});
      if(res.status != 200){
        this.$message.error('添加数据失败');
      }else{
        this.$message({
          message: '发布成功',
          type: 'success'
        });
        this.getCommentaryList();
      }
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleChange(value) {
      console.log(value);
    },
    // 每页条数
    handleSizeChange: function(newSize) {
      // console.log("newSize:"+newSize);
      this.commentarys.pagesize = newSize;
      this.getCommentaryList();
    },
    // 第几页
    handleCurrentChange: function(current) {
      // console.log("current:"+current);
      this.commentarys.pagenum = current;
      this.getCommentaryList();
    },
    // 修改按钮
    editOpen(item) {
      console.log(item);
      this.editCommentarys = item;
      this.deitFormVisible=true;
      this.editCommentarys.duration = item.zd_duration;
      this.editCommentarys.zd_id = Number(item.zd_id);
      this.getTypeId(this.editCommentarys.topic_type)
    },
    // 点击修改
    async edit(edit){
      this.deitFormVisible = false;
      console.log("已修改");
      edit.zd_duration = this.changeDuration(edit.duration);
      edit = JSON.stringify(edit);
      console.log(edit);
      const {data:res} = await this.$http.post("editCommentarys",{edit});
      console.log(res);
      if(res.status != 200){
        this.$message.error("修改信息失败");
      }else{
        this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.getCommentaryList();
      }

    },
    // 删除部分
    handleClose(done){
      // this.$confirm('确定关闭？').then(_ => {
      //   done();
      // }).catch(_ => {})
    },
    async del(id) {
      this.dialogVisible = false;
      console.log("删除了");
      console.log(id);
      let mesId = id;
      let type = '1';
      const {data:res} = await this.$http.post("deleteReview",{type,mesId});
      // // console.log(result);
      if(res.status !==200){
        return this.$message.error("添加失败")
      }else{
        this.$message.success("删除成功")
        this.getCommentaryList();
      }

    },
    // 转换时长
    changeDuration(duration){
      console.log(this.value);
      let type = this.value;
      console.log(duration);
      let zd_duration = "";
      switch(type){
        case "years":
          // alert("年");
          zd_duration = Number(duration)*365*24*60*60;
          break;
        case "months":
          // alert("月");
          zd_duration = Number(duration)*30*24*60*60;
          break;
        case "days":
          // alert("日");
          zd_duration = Number(duration)*24*60*60;
          break;
        case "hours":
          // alert("时");
          zd_duration = Number(duration)*60*60;
          break;
        case "minutes":
          // alert("分");
          zd_duration = Number(duration)*60;
          break;
        case "seconds":
          // alert("秒");
          zd_duration = Number(duration);
          break;
        default :
        alert("你的信息有误");
        break;
      }
      console.log(zd_duration);
      return zd_duration
    },
  }
}
</script>
<style scoped>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
  /* width: 150px; */
}
.box-card {
  margin: 10px 0;
}
.el-button {
  margin: 0;
}
.el-table {
  margin: 10px 0;
}
</style>
